home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / dev / amos / neural.lha / network.asc
Text File  |  1993-02-25  |  4KB  |  196 lines

  1. 'Amos 3 layer Neural Network V1.0
  2. '(C)1996 Lee Atkins. 
  3. '
  4. 'Only open a screen if you need to see anything. 
  5. '
  6. Screen Open 0,640,200,2,Hires
  7. Hide : Curs Off : Flash Off : Cls 0
  8. '
  9. 'ACC# is acceptable average squared error. 
  10. ACC#=0.008
  11. 'LAYERS is number of layers in network (including fanout layer). 
  12. 'Dont change yet!
  13. LAYERS=3
  14. 'HIDDEN is number of neurons in each hidden layer. 
  15. HIDDEN=4
  16. 'IPUT is number of inputs to network.
  17. IPUT=2
  18. 'OUTPUT is number of output neurons. 
  19. OUTPUT=1
  20. 'RATE# is the learning rate of the network.
  21. 'Bigger is not always better!
  22. RATE#=4
  23. 'PATTERNS is the total number of training patterns to use. 
  24. PATTERNS=4
  25. 'ITERATIONS is the number of times the entire training pattern is to 
  26. 'be presented to the network.
  27. ITERATIONS=640
  28. '
  29. '
  30. '
  31. Dim H#(HIDDEN)
  32. Dim F#(OUTPUT)
  33. Dim X(PATTERNS,IPUT)
  34. Dim R(PATTERNS,OUTPUT)
  35. '
  36. 'Setup current training set. 
  37. 'This is XNOR
  38. 'X(n,o) is input where n is pattern number and o is input number.
  39. 'R(p,q) is expected output where p is pattern and q is output number.
  40. '
  41. X(1,1)=0 : X(1,2)=0 : R(1,1)=1
  42. X(2,1)=0 : X(2,2)=1 : R(2,1)=0
  43. X(3,1)=1 : X(3,2)=0 : R(3,1)=0
  44. X(4,1)=1 : X(4,2)=1 : R(4,1)=1
  45. '
  46. Z=OUTPUT
  47. If HIDDEN>OUTPUT Then Z=HIDDEN
  48. Dim ERR#(LAYERS-2,Z+1)
  49. Dim W#(LAYERS-2,Z+1,Z+1)
  50. '
  51. 'Initialise initial weights for network
  52. '
  53. For L=0 To LAYERS-2
  54.    For N=1 To Z
  55.       For W=0 To Z-1
  56.          W#(L,N,W)=-2
  57.       Next W
  58.    Next N
  59. Next L
  60. '
  61. ITT=0
  62. MER#=1
  63. '
  64. 'Train the network.  
  65. '
  66. While ITT<=ITERATIONS and MER#>ACC#
  67.    For PAT=1 To PATTERNS
  68.       '
  69.       'Forward propagation 
  70.       '
  71.       For J=1 To HIDDEN
  72.          A#=W#(1,J,0)
  73.          For I=1 To IPUT
  74.             A#=A#+X(PAT,I)*W#(1,J,I)
  75.          Next I
  76.          H#(J)=1/(1+Exp(-A#))
  77.       Next J
  78.       '
  79.       For J=1 To OUTPUT
  80.          A#=W#(0,J,0)
  81.          For I=1 To HIDDEN
  82.             A#=A#+H#(I)*W#(0,J,I)
  83.          Next I
  84.          F#(J)=1/(1+Exp(-A#))
  85.       Next J
  86.       '
  87.       'Compute error for each output neuron
  88.       '  
  89.       For J=1 To OUTPUT
  90.          ERR#(0,J)=(R(PAT,J)-F#(J))*(F#(J)*(1-F#(J)))
  91.       Next J
  92.       '
  93.       'Adjust weights, hidden layer to output layer. 
  94.       '
  95.       For J=1 To OUTPUT
  96.          W#(0,J,0)=W#(0,J,0)+RATE#*ERR#(0,J)
  97.          For I=1 To HIDDEN
  98.             W#(0,J,I)=W#(0,J,I)+RATE#*ERR#(0,J)*H#(I)
  99.          Next I
  100.       Next J
  101.       '
  102.       'Compute error for each hidden neuron. 
  103.       '
  104.       For J=1 To HIDDEN
  105.          A#=0
  106.          For K=1 To OUTPUT
  107.             A#=A#+ERR#(0,K)*W#(0,K,J)
  108.          Next K
  109.          ERR#(1,J)=(H#(J)*(1-H#(J)))*A#
  110.       Next J
  111.       '
  112.       'Adjust weights form input layer to hidden layer 
  113.       '
  114.       For J=1 To HIDDEN
  115.          W#(1,J,0)=W#(1,J,0)+RATE#*ERR#(1,J)
  116.          For I=1 To IPUT
  117.             W#(1,J,I)=W#(1,J,I)+RATE#*ERR#(1,J)*X(PAT,I)
  118.          Next I
  119.       Next J
  120.    Next PAT
  121.    '
  122.    '
  123.    'Evaluate average squared error
  124.    '
  125.    MER#=0
  126.    For W=1 To PATTERNS
  127.       For H=1 To HIDDEN
  128.          A#=W#(1,H,0)
  129.          For Q=1 To IPUT
  130.             A#=A#+W#(1,H,Q)*X(W,Q)
  131.          Next Q
  132.          H#(H)=1/(1+Exp(-A#))
  133.       Next H
  134.       For H=1 To OUTPUT
  135.          A#=W#(0,H,0)
  136.          For Q=1 To HIDDEN
  137.             A#=A#+W#(0,H,Q)*H#(Q)
  138.          Next Q
  139.          F#(H)=1/(1+Exp(-A#))
  140.       Next H
  141.       For I=1 To OUTPUT
  142.          MER#=MER#+(R(W,I)-F#(I))^2
  143.       Next I
  144.       MER#=MER#/OUTPUT
  145.    Next W
  146.    MER#=MER#/PATTERNS
  147.    '
  148.    'The below is so you can see training progress.
  149.    '
  150.    'Print PAT;"  ";ITT;"   ";MER# 
  151.    Plot ITT*(640/ITERATIONS),200-MER#*400
  152.    '
  153.    '
  154.    ITT=ITT+1
  155. Wend 
  156. '
  157. 'Training complete!
  158. '
  159. '
  160. 'Print out opperating network
  161. '
  162. For W=1 To PATTERNS
  163.    Print 
  164.    For H=1 To HIDDEN
  165.       A#=W#(1,H,0)
  166.       For Q=1 To IPUT
  167.          A#=A#+W#(1,H,Q)*X(W,Q)
  168.          Print X(W,Q);"  ";
  169.       Next Q
  170.       H#(H)=1/(1+Exp(-A#))
  171.    Next H
  172.    For H=1 To OUTPUT
  173.       A#=W#(0,H,0)
  174.       For Q=1 To HIDDEN
  175.          A#=A#+W#(0,H,Q)*H#(Q)
  176.       Next Q
  177.       F#(H)=1/(1+Exp(-A#))
  178.       Print F#(H);"  ";
  179.    Next H
  180. Next W
  181. Wait Key 
  182. '
  183. 'Following is not needed if in another program.  Better save the 
  184. 'weights for later use.  saves training again. 
  185. '
  186. 'Print out weights 
  187. '
  188. Print : Print "Weights:"
  189. For L=0 To LAYERS-2
  190.    For N=1 To Z
  191.       For W=0 To Z-1
  192.          Print L;"  ";N;"   ";W;"    ";W#(L,N,W)
  193.       Next W
  194.    Next N
  195. Next L
  196.